<?php

require_once('functions/pageLoad.php');


if(isset($_GET['category']) && isset($_GET['url'])){

	$sql = "SELECT directory.id, directory.url, company_name, line_1, line_2, line_3, area, postcode, directory.latitude, directory.longitude, telephone, email, website, description FROM directory_to_cats 
			LEFT JOIN directory ON directory_to_cats.directory_id = directory.id 
			LEFT JOIN directory_cats ON directory_to_cats.category_id = directory_cats.id 
			LEFT JOIN areas ON directory.area_id = areas.id 
			WHERE approved = 1 AND directory.url = '".mysql_real_escape_string($_GET['url'])."' AND directory_cats.url = '".mysql_real_escape_string($_GET['category'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rs=mysql_fetch_assoc($query);
	
	$page_title = $rs['company_name'];
	$content = '<h1><a href="'.$rs['website'].'">'.$rs['company_name'].'</a></h1><p>'.$rs['description'].'</p><div id="tradesman_info">';

	$address.= (strlen($rs['line_1']) > 0) ? $rs['line_1'].'<br />' : '' ;
	$address.= (strlen($rs['line_2']) > 0) ? $rs['line_2'].'<br />' : '' ;
	$address.= (strlen($rs['line_3']) > 0) ? $rs['line_3'].'<br />' : '' ;
	$address.= (strlen($rs['area']) > 0) ? $rs['area'].'<br />' : '' ;
	$address.= (strlen($rs['postcode']) > 0) ? strtoupper($rs['postcode']) : '' ;

	if(strlen($rs['telephone']) > 0){
		$content.= '<div class="tradesman_info_wrap" style="margin-top: 10px;">
		<div class="tradesman_info_left">Telephone:</div>
		<div class="tradesman_info_right">'.$rs['telephone'].'</div>
		</div>';
	}
	

	if(strlen($address) > 0){

		$content.= '<div class="tradesman_info_wrap">
		<div class="tradesman_info_left">Address:</div>
		<div class="tradesman_info_right">'.$address.'</div>
		</div>';
		
	}
	
	$content.= '<div class="tradesman_info_wrap">
	<div class="tradesman_info_left">Website:</div>
	<div class="tradesman_info_right"><a href="'.$rs['website'].'" target="_blank">'.$rs['website'].'</a></div>
	</div>';


	if(strlen($address) > 0){

		$content.= '</div><div id="tradesman_map">';


		$additionalMeta.='
		<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key='.$settings['google_api_key'].'&amp;sensor=false" type="text/javascript"></script>
		<script type="text/javascript">
		function initialize(){
			if (GBrowserIsCompatible()) {
				var map = new GMap2(document.getElementById("tradesman_map"));
				map.setCenter(new GLatLng('.$rs['latitude'].', '.$rs['longitude'].'), 9);
				var marker = new GMarker(new GLatLng('.$rs['latitude'].', '.$rs['longitude'].'));
				map.addOverlay(marker);
			}
		}
		</script>';

		$bodyTag = ' onload="initialize()" onunload="GUnload()"';

	}
	
	$content.='</div>';

}

elseif(isset($_GET['category'])){

	$sql = "SELECT id, url, category FROM directory_cats WHERE url = '".mysql_real_escape_string($_GET['category'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rs=mysql_fetch_assoc($query);
	
	$page_title = $rs['category'];
	$content='<h1>'.$rs['category'].'</h1>';
	$category_id = $rs['id'];
	$category_url = $rs['url'];
	
	$sql = "SELECT company_name, url, description FROM directory_to_cats 
			LEFT JOIN directory ON directory_to_cats.directory_id = directory.id 
			WHERE directory_to_cats.category_id = '".$category_id."' AND approved = 1 
			ORDER BY company_name";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rows = mysql_num_rows($query);
	
	
	while($rs=mysql_fetch_assoc($query)){
	
	$content.='<article class="bd">
	<h1><a href="/directory/'.$category_url.'/'.$rs['url'].'">'.$rs['company_name'].'</a></h1>
	<p>'.substr($rs['description'], 0, 140).'</p>
	</article>';

	}

}

else{

$page_title = 'Business Directory';

	$sql = "SELECT url, category FROM directory_cats ORDER by category";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rows = mysql_num_rows($query);
	
	$half_way = ceil($rows / 2);
	
	$content='<h1>Business Directory</h1><p>Welcome to the I Want A Tradesman business directory. This list comprises of UK businesses in the trade industry, to start please select a category below.</p><p>If you would like to be a part of our directory, please <a href="/directory-registration">submit your business details for consideration</a>.</p>
	<h2 style="font-weight: normal">Categories</h2>
	<div style="float: left; width: 280px">
	<ul class="bd">';
	
	$i = 1;
	
	while($rs=mysql_fetch_assoc($query)){
	
	$content.=($i > $half_way) ? '</ul></div><div style="float: left; width: 280px; margin-left: 20px;"><ul class="bd">' : '' ;
	
	$content.='<li><a href="/directory/'.$rs['url'].'">'.$rs['category'].'</a></li>';
	
	$i++;
	
	}
	
	$content.='</ul>';


}

include('includes/meta.php');
include('includes/header.php');
include('includes/navigation.php');

?>

<?php echo $content; ?>

<?php 

include('includes/rightColumn.php');
include('includes/footer.php');

?>